package com.taptech.doufu.ui.view.viewflow.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.util.BitmapUtil;
import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.ui.activity.NewCpDesActivity;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.MainHomeActivity;
import com.taptech.doufu.ui.activity.NewAlbumsActivity;
import com.taptech.doufu.ui.activity.AnswerStarActivity;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.ui.activity.EasyBrowseActivity;
import com.taptech.doufu.bean.AlbumsBean;
import com.taptech.doufu.bean.HomeTopBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.services.CpService;
import com.taptech.doufu.services.PersonalNoteService;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.StartActivityUtil;
import com.taptech.doufu.util.TMAnalysis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ViewFlowAdImageAdapter extends BaseAdapter
{

    private List<HomeTopBean> imageList;// 存放图片解析数据
    private LayoutInflater mInflater;
    private Context mContext;
    //	private DisplayImageOptions options;
    //	private ImageLoader imageLoader = null;

    //	private ImageLoaderConfiguration configuration;
    private Bitmap defaultBitmap = null;
    static int width, space, rightSpace;

    public ViewFlowAdImageAdapter(Context context, List<HomeTopBean> list)
    {
        // TODO Auto-generated constructor stub

        this.mContext = context;
        this.imageList = list;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// 此处注意，保持文字一直存在
        if (width == 0)
        {
            space = ScreenUtil2.dip2px(6);
            rightSpace = ScreenUtil2.dip2px(8);

            int macthWidth = ScreenUtil2.SCREEN_PX_WIDTH - space * 3 - 2 * rightSpace;
            width = macthWidth / 3;

            defaultBitmap = BitmapUtil.decodeSampledBitmapFromResource(mContext.getResources(), R.drawable.bg_home_object_default, macthWidth, width);
        }

    }

    public void setData(List<HomeTopBean> list)
    {
        this.imageList = list;
    }

    @Override
    public int getCount()
    {
        // TODO Auto-generated method stub
        return Integer.MAX_VALUE;// 无限循环
    }

    @Override
    public Object getItem(int position)
    {
        // TODO Auto-generated method stub
        return imageList.get(position % imageList.size());
    }

    @Override
    public long getItemId(int position)
    {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        if (convertView == null)
        {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.pager_news_channel_list_header, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_pager_main_tab_news_header_banner);
            holder.titile = (TextView) convertView.findViewById(R.id.tv_pager_main_tab_news_header_title);
            convertView.setTag(holder);
        } else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        if (imageList == null || (imageList != null && imageList.size() == 0))
            return convertView;

        if (position % imageList.size() >= 0 && position % imageList.size() < imageList.size())
        {
            //
            HomeTopBean bean = imageList.get(position % imageList.size());
            holder.imageView.setImageBitmap(defaultBitmap);
            holder.titile.setText(bean.getTitle());
            if (bean.getImages() != null)
            {
                ImageManager.displayImage(holder.imageView, imageList.get(position % imageList.size()).getImages()[0].getImgUrl(), 1);
            }
        }

        convertView.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                HomeTopBean bean = imageList.get(position % imageList.size());
                if (bean == null)
                {
                    return;
                }
                Intent intent = new Intent();
                switch (DiaobaoUtil.String2Int(bean.getObject_type()))
                {
                    case Constant.OBJECT_TYPE_NOVEL:
                        StartActivityUtil.novelOnclick(mContext, bean.getId());
                        break;

                    case Constant.OBJECT_TYPE_ANSWER:
                        if (AccountService.getInstance().isLogin())
                        {
                            intent.setClass(mContext, AnswerStarActivity.class);
                            mContext.startActivity(intent);
                        } else
                        {
                            AccountService.getInstance().jumpToLogin();
                        }
                        break;
                    case Constant.OBJECT_TYPE_BROWSE:
                        String url = bean.getUrl();
                        String title = bean.getTitle();
                        intent.setClass(mContext, BrowseActivity.class);
                        intent.putExtra(Constant.URL, url);
                        intent.putExtra(Constant.TITLE, title);
                        mContext.startActivity(intent);
                        break;
                    case Constant.OBJECT_TYPE_EASY_WEBVIEW:
                        intent.setClass(mContext, EasyBrowseActivity.class);
                        intent.putExtra(Constant.URL, bean.getUrl());
                        intent.putExtra(Constant.TITLE, bean.getTitle());
                        mContext.startActivity(intent);
                        break;
                    case Constant.OBJECT_TYPE_ALBUMS:
                        AlbumsBean albumsBean = new AlbumsBean();
                        albumsBean.setType(bean.getType());
                        albumsBean.setName(bean.getTitle());
                        albumsBean.setUrl(bean.getUrl());
                        StartActivityUtil.albumsOnclick(mContext, albumsBean);
                        break;
                    case Constant.CONTENT_TYPE_TOPIC:
                        switch (DiaobaoUtil.String2Int(bean.getTopic_type()))
                        {
                            case Constant.OBJECT_TYPE_NOVEL:
                                StartActivityUtil.novelOnclick(mContext, bean.getId());
                                break;

                            case Constant.OBJECT_TYPE_ANSWER:
                                if (AccountService.getInstance().isLogin())
                                {
                                    intent.setClass(mContext, AnswerStarActivity.class);
                                    mContext.startActivity(intent);
                                } else
                                {
                                    AccountService.getInstance().jumpToLogin();
                                }
                                break;
                            case Constant.OBJECT_TYPE_SWEEP:
                                if (AccountService.getInstance().isLogin())
                                {
                                    int type = DiaobaoUtil.String2Int(bean.getType());
                                    intent.setClass(mContext, NewAlbumsActivity.class);
                                    intent.putExtra(Constant.URL, bean.getUrl());
                                    intent.putExtra(Constant.TYPE, type);
                                    intent.putExtra(Constant.NAME, bean.getName());
                                    mContext.startActivity(intent);
                                } else
                                {
                                    AccountService.getInstance().jumpToLogin();
                                }
                                break;
                            default:
                                break;

                        }
                        break;
                    case Constant.OBJECT_TYPE_NOTE:
                    case Constant.OBJECT_TYPE_DRAW:
                    case Constant.OBJECT_TYPE_COS:
                        PersonalNoteService.enterPersonalNoteDesActivity(mContext, bean.getId(), bean.getObject_type());
                        break;
                    case Constant.OBJECT_TYPE_CP:
                        CpService.enterCpDesActivity(mContext, bean.getId());
                        break;
                    case Constant.OBJECT_TYPE_CARTOON:
                        CartoonServices.enterCartoonDes(mContext, bean.getId());
                        break;
                }
                try
                {
                    Activity activity = (Activity) mContext;
                    // 轮播图统计事件
                    Map<String, String> analysisMap = new HashMap<String, String>();
                    analysisMap.put("mpositon", position % imageList.size() + "");
                    analysisMap.put("mid", bean.getId());
                    analysisMap.put("mtype", bean.getObject_type());
                    //  TMAnalysis.event(mContext, "home-top_gallery", analysisMap);
                    if (activity instanceof MainHomeActivity)
                    {
                        switch (DiaobaoUtil.String2Int(bean.getObject_type()))
                        {
                            case Constant.OBJECT_TYPE_CP:
                                TMAnalysis.event(mContext, "cp_top_gallery", analysisMap);
                                break;
                            default:
                                TMAnalysis.event(mContext, "home-top_gallery", analysisMap);
                                break;
                        }

                    } else if (activity instanceof NewCpDesActivity)
                    {
                        TMAnalysis.event(mContext, "cp_top", analysisMap);
                    } else //if (activity instanceof VideoListActivity)
                    {
                        TMAnalysis.event(mContext, "video_top", analysisMap);
                    }
                } catch (Exception e)
                {   //说明是ApplicationContext
                    e.printStackTrace();
                }
            }
        });
        return convertView;
    }

    public static class ViewHolder
    {
        ImageView imageView;
        TextView titile;
    }

    public void setSourceDate(List<HomeTopBean> imageList)
    {
        this.imageList = imageList;
        notifyDataSetChanged();
    }
}
